package com.macrounion.atv.processor;

import com.macrounion.atv.service.entity.Protocol;
import com.macrounion.atv.service.service.ProtocolService;
import com.macrounion.base.service.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.macrounion.atv.service.entity.Net;
import com.macrounion.atv.service.service.NetService;
import com.macrounion.base.processor.BaseProcessor;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
public class NetProcessor extends BaseProcessor<Net, Net, NetService, Net> {

    @Autowired
    private ProtocolService protocolService;
    @Transactional
    @Override
    public Net save(Net reqDto) {
        //检查网关id不能重复
        int findNet=service.findNet(reqDto);
        if(findNet>0)
            throw new BusinessException(201,"已存在相同的网关id");
        return super.save(reqDto);
    }

    @Transactional
    @Override
    public boolean removeById(String id) {
        // TODO Auto-generated method stub
        //判断网关名称是否已用，有则不能删除
        List<Protocol> protocols=protocolService.getProtocolByNetId(id);
        if(protocols!=null && protocols.size()>0)
            throw new BusinessException(201,"该网关已有使用的协议，不能删除");

        return super.removeById(id);
    }


}